﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace Qiang.Styles
{
    public class LabelStyle : Style ,ILabelStyle
    {

        #region 初始化

        /// <summary>
        /// 
        /// </summary>
        public LabelStyle()
            :base()
        {
            _color = Color.FromArgb(Random.Next(255), Random.Next(255), Random.Next(255));
            _font = new Font("", 8.0f);
            _format = new StringFormat();
            _position = LabelPosition.Center;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="color"></param>
        /// <param name="size"></param>
        public LabelStyle(Color color, float size)
        {
            _color = color;
            _font = new Font("", size);
        }

        #endregion


        #region 属性

        Color _color;

        public Color Color
        {
            get { return _color; }
            set { _color = value; }
        }

        Font _font;

        public Font Font
        {
            get { return _font; }
            set { _font = value; }
        }

        StringFormat _format;

        public StringFormat Format
        {
            get { return _format; }
            set { _format = value; }
        }


        LabelPosition _position;

        public LabelPosition Position
        {
            get { return _position; }
            set { _position = value; }
        }

        
        public override float Opacity
        {
            get
            {
                return _color.A / 255f;
            }
            set
            {
                float val = value;
                if (val > 1) val = 1f;
                if (val < 0) val = 0f;
                _color = Color.FromArgb((int)val * 255, _color);
            }
        }

        #endregion

        #region 方法

        /// <summary>
        /// 
        /// </summary>
        /// <param name="g"></param>
        /// <param name="str"></param>
        /// <param name="position"></param>
        public void DrawString(Graphics g, string str,PointF point)
        {
            Brush brush = ToBrush();
            PointF position = ComputePosition(g, str, point);
            g.DrawString(str, _font, brush, position, Format);
        }

        /// <summary>
        /// 
        /// </summary>
        Brush ToBrush()
        {
            Brush brush = new SolidBrush(_color);
            return brush;
        }


        PointF ComputePosition(Graphics g, string str, PointF point)
        {
            PointF position = new PointF(point.X, point.Y);
            SizeF size = g.MeasureString(str, _font);
            switch (_position)
            {
                case LabelPosition.Bottom:
                    position.X = position.X - size.Width / 2;
                    break;
                case LabelPosition.Center:
                    position.X = position.X - size.Width / 2;
                    position.Y = position.Y - size.Height / 2;
                    break;
                case LabelPosition.Left:
                    position.X = position.X - size.Width;
                    position.Y = position.Y - size.Height / 2;
                    break;
                case LabelPosition.LeftBottom:
                    position.X = position.X - size.Width;
                    break;
                case LabelPosition.LeftUp:
                    position.X = position.X - size.Width;
                    position.Y = position.Y - size.Height;
                    break;
                case LabelPosition.Right:
                    position.Y = position.Y - size.Height / 2;
                    break;
                case LabelPosition.RightBottom:

                    break;
                case LabelPosition.RightUp:
                    position.Y = position.Y - size.Height;
                    break;
                case LabelPosition.Up:
                    position.X = position.X - size.Width / 2;
                    position.Y = position.Y - size.Height;
                    break;
                default:
                    break;

            }
            return position;
        }

        #endregion
    }
}
